﻿layui.use(['tree', 'form', 'layedit', 'laydate'], function () {
    var form = layui.form()
        , layer = layui.layer;


    var ztree;
    var setting = {
        view: {
            checkable: true,
            selectedMulti: false
        },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType:{ "Y" : "ps", "N" : "ps" },
        },
        data: {
            key: {
                children:"children",
                name: "name",
            }
        }
        //async: {
        //    enable:true,
        //    url: '/Manage/GetAllPermissions',
        //    dataType: "text",
        //    autoParam: ["id", "name", "code", "nodes"]
        //}
    };
    var zTreeNodes = new Array();
    $(document).ready(function () {
       $.ajax({
           url: '/Manage/GetAllPermissions',
           type: 'Get',
           dataType:'json',
           success: function (data) {
               //for(var i = 0;i < data.length;i++){
               //    zTreeNodes.push(data[i]);
               //}
               zTreeNodes = data;
               ztree = $.fn.zTree.init($("#permissions_tree"), setting, zTreeNodes);
            }
        })
    });

    $("#add-submit").click(function () {
        var ids = GetSelectPermissions("permissions_tree");
        var index = layer.load();
        $.ajax({
            url: '/Manage/AddEmpolyee',
            method:"post",
            data: {
                Name: $("#employee-name").val(),
                PhoneNumber: $("#employee-phonenumber").val(),
                Email: $("#employee-email").val(),
                Gender: $("input[type='radio']:checked").val(),
                Permissions: ids,
            },
            success: function (data) {
                if (data.success != "") {
                    layer.msg(data.success);
                }
                else {
                    layer.msg(data.error);
                }
                layer.close(index);
            },
            error: function () {
                layer.msg("服务端连接异常");
                layer.close(index);
            }
        })
    })

    //form.on('submit(addEmployee)', function (data) {
    //    data.field.Gender = $("input[type='radio']:checked").val();
    //    var ids = GetSelectPermissions("permissions_tree");
    //    var index = layer.load();
    //    $.ajax({
    //        url: '/Manage/AddEmpolyee',
    //        data: {employee:data.field,permissions:ids},
    //        success: function (data) {
    //            if (data.success != "") {
    //                layer.msg(data.success);
    //            }
    //            else {
    //                layer.msg(data.error);
    //            }
    //            layer.close(index);
    //        },
    //        error: function () {
    //            layer.msg("服务端连接异常");
    //            layer.close(index);
    //        }
    //    })
    //    return false;
    //})


})

function GetSelectPermissions(tag) {
    var treeObj = $.fn.zTree.getZTreeObj(tag);
    var nodes = treeObj.getCheckedNodes(true);

    var permissionIds = new Array();
    for (var i = 0; i < nodes.length; i++) {
        permissionIds.push(nodes[i].id);
    }
    return permissionIds;
}